/**
Zeke Elder - zbe@andrew.cmu.edu
Amal Chemmany - achemman@andrew.cmu.edu
Nishant Kumar - nkumar1@andrew.cmu.edu
15-637
4/2/2012
 **/

package edu.cmu.fithub.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import edu.cmu.fithub.controller.Action;
import edu.cmu.fithub.db.DBHelper;
import edu.cmu.fithub.db.bean.Friend;
import edu.cmu.fithub.db.bean.User;

public class DisplayFriendAction extends Action {

	public DisplayFriendAction() {
	}

	public String getName() {
		return "displayFriends.do";
	}

	public String perform(HttpServletRequest request) {

		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);

		User user = (User) request.getSession().getAttribute("user");
		Session dbSession = DBHelper.getSessionFactory().openSession();
		List list = dbSession.createCriteria(Friend.class)
				.add(Restrictions.eq("userId", user.getUserId())).list();
		Friend[] friendList = new Friend[list.size()];
		friendList = (Friend[]) list.toArray(friendList);
		request.setAttribute("friendList", friendList);
		return "friends.jsp";
	}
}
